package com.chelun.support.clmutiplyfragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chelun.support.clmutiplyfragment.model.ListItemBean;
import com.chelun.support.clutils.utils.CheckUtils;
import com.hannesdorfmann.adapterdelegates3.AdapterDelegatesManager;

import java.util.List;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;


/**
 * Created by leo on 16/4/19.
 *
 * 多元化fragment
 */
public abstract class MultiFunctionFragment extends Fragment {

    private RecyclerView mRecyclerView;
    private MultiFunctionListAdapter mAdapter;

    /**
     * 每个用例的管理类
     */
    private AdapterDelegatesManager<List<ListItemBean>> mDelegatesManager;
    protected ViewGroup mRootView;
    protected ClMultiChelunPtrRefresh mPtrRefresh;
    protected ClMultiYFootView mFootView;
    private ViewGroup mTopView;
    private ViewGroup mFlError;
    private ViewGroup mFlWholeError;


    /**
     * 初始化回调 ,时机是在onViewCreated（）
     */
    public abstract void onInit(Bundle savedInstanceState);

    /**
     * 刷新时调用
     */
    public abstract void onRefresh();

    /**
     * 加载时调用
     */
    public abstract void onLoadMore();


    /**
     * 加载更多时调用
     */
    public abstract void onErrorLoadMore();

    protected MultiFunctionListAdapter getAdapter() {
        return mAdapter;
    }

    /**
     * 添加adapter的delegates
     *
     * @param delegatesManager 第三方的adapteraDelegates
     */
    public abstract void addAdapterDelegate(AdapterDelegatesManager<List<ListItemBean>> delegatesManager);

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = (ViewGroup) inflater.inflate(R.layout.fragment_multi_fuction, null);
            initViews(mRootView);
        }
        return mRootView;
    }

    protected  void initViews(ViewGroup rootView){
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.mutiRecyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mPtrRefresh = (ClMultiChelunPtrRefresh) rootView.findViewById(R.id.clMulti_main_ptr_frame);
        mTopView = (ViewGroup) rootView.findViewById(R.id.clMultiflTop);
        mFlError = (ViewGroup)rootView.findViewById(R.id.clMultiflError);
        mFlWholeError = (ViewGroup)rootView.findViewById(R.id.clMultiflWholeError);
        mDelegatesManager = new AdapterDelegatesManager<>();
        addAdapterDelegate(mDelegatesManager);
        mPtrRefresh.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                onRefresh();
            }
        });
        mFootView = new ClMultiYFootView(getActivity(), R.drawable.clmulti_selector_list_item_white_gray, mRecyclerView);
        mFootView.setOnMoreListener(new ClMultiYFootView.OnMoreListener() {
            @Override
            public void getMore() {
                onLoadMore();
            }
        });
        mPtrRefresh.disableWhenHorizontalMove(true);
        mAdapter = new MultiFunctionListAdapter(mDelegatesManager,mFootView);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        onInit(savedInstanceState);
    }

    /**
     * 设置item至adapter,建议刷新使用,不建议加载更多使用
     */
    public void setItem(List<ListItemBean> list) {
        if (CheckUtils.isNotNull(mAdapter)) {
            mAdapter.setItem(list);
            setRefreshComplete();
        }
    }


    /**
     * 为加载更多提供的专用方法，效率高
     */
    public void addLoadMoreItem(List<ListItemBean> list) {
        loadMoreComplete();
        mAdapter.addLoadMoreItem(list);
    }


    /**
     * 该次加载更多的动作完毕
     */
    public void loadMoreComplete() {
        mFootView.refreshMoreOver(false);
    }

    /**
     * 下拉刷新关闭
     */
    public void setRefreshComplete() {
        mPtrRefresh.refreshComplete();
    }


    /**
     * 全部内容加载完
     */
    protected void loadMoreEnd() {
        mFootView.refreshMoreOverHideFoot();
    }

    /**
     * 恢复为正常加载的状态
     */
    protected void loadMoreRestore() {
        mFootView.refreshMoreOver(false);
    }

    /**
     *  设置是否有loadmore
     * @param hasLoadMore
     */
    protected void setHasLoadMore(boolean hasLoadMore) {
        mAdapter.setHasLoadMore(hasLoadMore);
    }

    /**
     *  显示加载错误时的信息
     * @param msg
     */
    protected void showErrorMsg(String msg) {
        mFootView.refreshMoreOver(msg, true);
    }

    protected void showErrorMsg() {
        showErrorMsg("点击重新加载");
    }

    protected RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    protected ViewGroup getTopView() {
        return mTopView;
    }

    protected ViewGroup getErrorView(){
        return mFlError;
    }

    protected ViewGroup getWholeErrorView(){
        return mFlWholeError;
    }
}
